package com.holike.crm.bean;

import android.app.Activity;

import com.google.gson.annotations.SerializedName;
import com.holike.crm.util.DensityUtil;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class MessageDetailsBean implements Serializable {
    /**
     * createDate : 2018-02-05
     * name : 王芳莉
     * pictureList : ["http://120.79.76.243/notice/noticeImg/关于卡普里系列DK床淘汰替换的通知_0001.jpg","http://120.79.76.243/notice/noticeImg/5.png"]
     * title : 关于卡普里系列DK床淘汰替换的通知
     */

    public String createDate;
    public String name;
    public String title;
    @SuppressWarnings("WeakerAccess")
    List<String> pictureList;
    @SerializedName("urlbase")
    public String baseUrl;
    @SuppressWarnings("WeakerAccess")
    String html;

    /**
     * Jsoup解析html字符串
     * 设置img标签下的width为手机屏幕宽度，height自适应
     *
     * @return 更新宽高属性后的html字符串
     */
    public String getHtml(Activity activity) {
        try {
            Document document = Jsoup.parse(html);
            Elements pElements = document.select("p:has(img)");
            for (Element pElement : pElements) {
                pElement.attr("style", "text-align:center");
                pElement.attr("max-width", DensityUtil.getWindowWidth(activity) + "px").attr("height", "auto");
            }
            Elements imgElements = document.select("img");
            for (Element imgElement : imgElements) {
                //重新设置宽高
                imgElement.attr("max-width", "100%").attr("height", "auto");
                imgElement.attr("style", "max-width:100%;height:auto");
            }
            return document.toString();
        } catch (Exception e) {
            return null;
        }
    }

    @Deprecated
    public List<String> getPictureList() {
        return pictureList == null ? new ArrayList<>() : pictureList;
    }
}
